var totalPage;
var totalCnt;
var onDutyLogData = [];
var deleteflag ;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	selectDate("startDate","0");
	selectDate("endDate","0");
	setTimeout(hideLoading, 500);
})
function search() {
	clearTable("dutyLogTbody");
	seach=1;
	showLoading();
	$.ajax({
		type : 'post',
		url : 'searchOnDutyLog',
		dataType : 'json',
		data : $("#dutyLogSearch").serialize(),
		success : function(data) {
			setTimeout(hideLoading, 500);
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			onDutyLogData = data && data.dutyLogList || [];
			document.getElementById("total").innerText = '共' + totalCnt + '条 / 共' + totalPage + '页';
			// 查询数据的数组长度
			var length = onDutyLogData.length;
			// 当前页数
			var currentPage = $('#currentPage').val();
			// 每页显示的条数
			var rows = $('#rows').val();
			// 画面表格的body
			var tbody = $("#dutyLogTbody");
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
				for(var i=0; i<length; i++){
					// 序号
					var rowNo = (currentPage - 1) * rows + i + 1;
					// 追加显示数据
					var $tr = $('<tr>').appendTo(tbody);
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(timeStamp2String(onDutyLogData[i].dutyTime)).appendTo($tr);
					$("<td>").html(timeStamp2String(onDutyLogData[i].takeTime)).appendTo($tr);
					$("<td>").html(onDutyLogData[i].dutyMan).appendTo($tr);
					$("<td>").html(onDutyLogData[i].workRecord).appendTo($tr);
					$("<td>").html(onDutyLogData[i].situationRecord).appendTo($tr);
					$("<td>").html(onDutyLogData[i].successor).appendTo($tr);
					$("<td>").html(onDutyLogData[i].todo).appendTo($tr);
				}
			}
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("您没有操作权限!");
			} else {
				showMessge("查询失败!");
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}
function save(){
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '值班日志信息添加';
    $("#save").modal("show");
    selectDate("saveDutyDate","0");
    selectDate("saveTakeDate","0");
    deleteflag = "save";
    saveTitle.append(strong);
	formReset("saveForm");
}
function confirm(){
	var saveDutyTime = $("#saveDutyTime").val();
	var saveTakeTime = $("#saveTakeTime").val();
	var saveDutyMan = $("#saveDutyMan").val();
	var saveWorkRecord = $("#saveWorkRecord").val();
	var saveSituationRecord = $("#saveSituationRecord").val();
	var saveSuccessor = $("#saveSuccessor").val();
	var saveTodo = $("#saveTodo").val();
	if (!saveDutyTime) {
		showMessge("值班时间不能为空！");
		return;
	}
	if (!saveTakeTime) {
		showMessge("接班时间不能为空！");
		return;
	}
	if (!saveDutyMan) {
		showMessge("值班人不能为空！");
		return;
	}
	if (!saveWorkRecord) {
		showMessge("当班工作记录不能为空！");
		return;
	}
	if (!saveSituationRecord) {
		showMessge("交接班情况记录不能为空！");
		return;
	}
	if (!saveSuccessor) {
		showMessge("接班人不能为空！");
		return;
	}
	if (!saveTodo) {
		showMessge("待办事宜不能为空！");
		return;
	}
	$('#save').modal('hide');
		if(deleteflag == "save"){
			showLoading();
			$.ajax({
				type: "POST",
				url: 'saveOnDutyLog',
		    	data: {
		    		DutyDate : saveDutyTime,
		    		TakeDate : saveTakeTime,
		    		DutyMan : saveDutyMan,
					WorkRecord : saveWorkRecord,
					SituationRecord : saveSituationRecord,
					Successor : saveSuccessor,
					Todo : saveTodo,
		    	},
		    	dataType : 'json',
				success: function(data){
					setTimeout(hideLoading, 500);
					var result = data && data.result;
					if (result == "Success") {
							showMessge("添加成功");
					} else if (result == "NotFound") {
						showMessge("添加失败");
					} else {
						showMessge("添加异常");
					}
					search();
					formReset("saveForm");
				},
				error : function(data) {
				setTimeout(hideLoading, 500);
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge("您没有操作权限!");
					} else {
						showMessge("添加失败!");
					}
				},
				timeout: function(data){
					setTimeout(hideLoading, 500);
					showMessge("操作超时！");
				}
			});
		}
	}
